﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Saving and loading the compiler </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="Saving and loading the compiler ">
    <meta name="generator" content="docfx 2.42.4.0">
    
    <link rel="shortcut icon" href="../favicon.ico">
    <link rel="stylesheet" href="../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../styles/docfx.css">
    <link rel="stylesheet" href="../styles/main.css">
    <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> 
    <meta property="docfx:navrel" content="../toc.html">
    <meta property="docfx:tocrel" content="toc.html">
    
    
    
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
  <script defer="" src="https://code.getmdl.io/1.3.0/material.min.js"></script>
  </head>  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              
              <a class="navbar-brand" href="../index.html">
                <img id="logo" class="svg" src="../logo.svg" alt="">
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
            </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div role="main" class="container body-content hide-when-search">
        
        <div class="sidenav hide-when-search">
          <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
          <div class="sidetoggle collapse" id="sidetoggle">
            <div id="sidetoc"></div>
          </div>
        </div>
        <div class="article row grid-right">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="">
<h1 id="saving-and-loading-the-compiler">Saving and loading the compiler</h1>

<p>Saving the compiler after compiling the kernel and reusing it is an efficient way of working. This save a lot of compilation time and the saved bin file can be reused in other project.</p>
<p>Below is how you can save a compiler after compiling with kernel class. Simply invoke Save function passing the file name:</p>
<pre><code class="lang-csharp">private void SaveCompiler()
{
    //Create instance of OpenCL compiler
    var compiler = new OpenCLCompiler();

    //Select a default device
    compiler.UseDevice(0);

    //Compile the sample kernel
    compiler.CompileKernel(typeof(SimpleKernels));
    compiler.Save(&quot;amp_cl.bin&quot;);
}
</code></pre>
<p>Create another project file, and you can copy the amp_cl.bin file to the new project directory. Below is the code to load and execute, note that you don't have to compile anything here.</p>
<pre><code class="lang-csharp">//Once saved you can reuse the same bin instead of compiling from scratch. Save compilation time. Also the bin file is portable
var compiler = new OpenCLCompiler();
compiler.Load(&quot;amp_cl.bin&quot;);

foreach (var item in compiler.Kernels)
{
    Console.WriteLine(item);
}

//Create variable a, b and r
Array x = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Array y = new float[9];

//Get the execution engine
var exec = compiler.GetExec&lt;float&gt;();

//Execute fill kernel method
exec.Fill(y, 0.5f);

//Execuete SAXPY kernel method
exec.SAXPY(x, y, 2f);

//Print the result
Console.WriteLine(&quot;\nResult----&quot;);
for (int i = 0; i &lt; y.Length; i++)
{
    Console.Write(y.GetValue(i) + &quot; &quot;);
}
</code></pre>
</article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                  <li>
                    <a href="https://github.com/tech-quantum/Amplifier.NET/blob/master/doc-gen/articles/Save and load.md/#L1" class="contribution-link">Improve this Doc</a>
                  </li>
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
              <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
              </nav>
            </div>
          </div>
        </div>
      </div>
      
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            
            <span>Generated by <strong>DocFX</strong></span>
          </div>
        </div>
      </footer>
    </div>
    
    <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../styles/docfx.js"></script>
    <script type="text/javascript" src="../styles/main.js"></script>
  </body>
</html>
